package com.navinfo.tripanalysis.offline.service.impl;

import com.navinfo.tripanalysis.common.arithmetic.common.OuterEventData;
import com.navinfo.tripanalysis.common.util.DateUtils;
import com.navinfo.tripanalysis.offline.service.SaveTripEventService;
import com.navinfo.tripanalysis.offline.service.TripEventConvertService;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.StructType;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 保存事件数据到hive中
 * @author 沈东生
 */
public class SaveTripEventServiceHiveImpl extends AbstractHiveSaveService implements SaveTripEventService {
    private TripEventConvertService convertService;

    public SaveTripEventServiceHiveImpl(TripEventConvertService convertService) {
        this.convertService = convertService;
    }

    @Override
    public void save(SparkSession spark, JavaSparkContext jsc, JavaPairRDD<Long, List<OuterEventData>> eventRDD, long d) {
        long start = System.currentTimeMillis();
        logger.error("进行行程事件数据落盘，保存Hive开始...");

        //保存数据
        String day = DateUtils.format(new Date(d), DateUtils.DateFormat.YYYYMMDD);
        super.saveData(spark, eventRDD, hdfsPath +"/day="+day, false, Collections.singletonList("day"),  Collections.singletonList("'"+day+"'"));

        logger.error("进行行程事件数据落盘，保存Hive耗时" + (System.currentTimeMillis() - start) + "ms");
    }

    @Override
    protected StructType createSchema() {
        return convertService.createSchema();
    }

    @Override
    protected JavaRDD<Row> createRowRDD(JavaPairRDD resourceRDD) {
        return resourceRDD.flatMap((FlatMapFunction<Tuple2<Long, List<OuterEventData>>, Row>) longListTuple2 -> {
            List<OuterEventData> srcList = longListTuple2._2;
            List<Row> list = new ArrayList<>(srcList.size());
            for (OuterEventData m : srcList) {
                list.add(convertService.toRow(m, null));
            }
            return list.iterator();
        });
    }
}
